
-- ASCII转HEX字符串，并根据指定字符进行分隔
function AsciitoHexStr(str, char)
    local i, hexstr = 1, ""
    for i = 1, string.len(str) do
        hexstr = hexstr .. string.sub(str, i, i):toHex()
        if i < string.len(str) then hexstr = hexstr .. char end
    end
    return hexstr
end

-- HEX转HEX字符串，并根据指定字符进行分隔
function HextoHexStr(hex, char)
    local i, hexstr = 1, ""
    for i = 1, #hex do
        hexstr = hexstr .. string.format("%02X", hex[i])
        if i < #hex then hexstr = hexstr .. char end
    end
    return hexstr
end

-- HEX字符串分割
function Split(str, pattern)
    local i, ret = 1, {}
    for item in string.gmatch(str, "(.-)" .. pattern) do
        ret[i] = item
        i = i + 1
    end
    return ret
end

-- HEX字符串转数组
function HexStrToTable(str)
    local i, buf = 1, {}
    -- “123456”字符串分解为‘1’‘2’‘3’‘4’‘5’‘6’
    for i = 1, string.len(str) do buf[i] = string.byte(string.sub(str, i, i)) end
    return buf
end

-- NumBer数组转NumBer
function TableToNumber(tab, sta, len)
    local i, str = 1, ""
    if i + sta + len > #tab then return -1 end
    for i = 1, len do str = str .. string.char(tab[i + sta]) end
    return tonumber(str)
end

-- NumBer数组转String
function TableToString(tab, sta, len)
    local i, str = 1, ""
    if sta + len > #tab then return -1 end
    for i = 1, len do str = str .. string.char(tab[i + sta]) end
    return str
end

-- 日期字符串转时间戳
function DateStrToSec(str)
    local statime = os.time({
        year = tonumber(string.sub(str, 1, 2)) + 2000,
        month = tonumber(string.sub(str, 3, 4)),
        day = tonumber(string.sub(str, 5, 6)),
        hour = tonumber(string.sub(str, 7, 8)),
        minute = 0,
        second = 0
    })
    return statime + tonumber(string.sub(str, 9, 10)) * 60 +
        tonumber(string.sub(str, 11, 12))
end

-- NumBer保留N位小数
function GetPreciseDecimal(num, n)
    local ret = string.format("%5." .. n .. "f", num)
    return tonumber(ret)
end

function Crc16_Checkout(data)
    local crc_reg, check
    crc_reg = 0xFFFF
    for i = 1, #data do
        local c = data:byte(i)
        crc_reg = bit.bxor(bit.rshift(crc_reg, 8), c)
        for j = 0, 7 do
            check = bit.band(crc_reg, 0x0001)
            crc_reg = bit.rshift(crc_reg, 1)
            if check == 0x0001 then
                crc_reg = bit.bxor(crc_reg, 0xA001)
            end
        end
    end
    return crc_reg
end

--- utf8编码 转化为 gb2312编码
-- @string utf8s utf8编码数据
-- @return string data,gb2312编码数据
-- @usage local data = common.utf8ToGb2312(utf8s)
function utf8ToGb2312(utf8s)
    local cd = iconv.open("ucs2", "utf8")
    local ucs2s = cd:iconv(utf8s)
    cd = iconv.open("gb2312", "ucs2")
    return cd:iconv(ucs2s)
end
-- function CRC.Hash(s)
--     assert(type(s) == 'string')
--     local crc = 0xffff
--     for i = 1, #s do
--         local c = s:byte(i)
--         crc = bxor(crc, c)
--         for j = 1, 8 do
--             local k = band(crc, 1)
--             crc = rshift(crc, 1)
--             if k ~= 0 then
--                 crc = bxor(crc, 0x8408)
--             end
--         end
--     end
--     return crc
-- end
-- Queue={first=0,last=-1}
-- function Queue.pushleft(value)
--     local first=Queue.first-1
--     Queue.first=first
--     Queue[first]=value
-- end
-- function Queue.pushright(value)
--     local last=Queue.last+1
--     Queue.last=last
--     Queue[last]=value
-- end
-- function Queue.popleft()
--     local first=Queue.first
--     if first>Queue.last then error("queue is empty") end
--     local value=Queue[first]
--     Queue[first]=nil
--     Queue.first=first+1
--     return value
-- end
-- function Queue.popright()
--     local last=Queue.last
--     if Queue.first>last then error("queue is empty") end
--     local value=Queue[last]
--     Queue[last]=nil
--     Queue.last=last-1
--     return value
-- end


